<template>
  <div>
     <el-table
      :data="tableData"
      height="250"
      border
      style="width: 100%;">
      <el-table-column
        prop="Nickname"
        label="昵称"
        width="180">
      </el-table-column>
      <el-table-column
        prop="nowTime"
        label="日期"
        width="280">
      </el-table-column>
      <el-table-column
        prop="price"
        label="出价">
      </el-table-column>
      <el-table-column
        prop="userPhone"
        label="手机号">
      </el-table-column>
    </el-table>
    <!-- <div>{{goodsList.goodsId}}---{{goodsId}}</div> -->
  </div>
</template>

<script>
import axios from 'axios'
export default {
    name: "JpPersonList",
    props:['goodsList'],
    data() {
      return {
        goodsId:'',
        tableData: [],
        orderlist:{},
        firstUser:{},
        userName:'',
        endTime:'',//接收goodsInfo传值的商品结束时间
        userMoney:[],//用户余额
        time:'',//剩余时间
        timer:null,
        allSecond:'',
        flag:false,
        endData:'',//支付结束时间
      }
    },
    created() {
      
      this.$bus.$on('getEndtime',(val)=>{
          this.endTime = val;
          // console.log("接收的结束时间为",val)
      })
      this.$bus.$on('getName',(val)=>{
          this.userName = val;
          // console.log("接收的用户名为",val)
      })
    },
    mounted() {
      this.getOrder()
      this.getAuction()
      console.log("商品id为与结束时间与用户名",this.goodsId,this.endTime,this.goodsList,this.userName)
    },
    updated() {
      this.addOrder()
      this.updatedMoney()
    },
    beforeDestroy() {
      clearTimeout(this.timer)
    },
    methods: {
        // 根据商品获取订单
        getOrder(){
          this.goodsId = this.goodsList.goodsId
          axios.get("http://localhost:8080/api/getOrder/getOrderlist",{
            params:{
              goodsId:this.goodsId
            }
          }).then(
            res=>{
              let result = res.data
              this.orderList = result
              console.log("订单信息",this.orderList,result)

              console.log("获取结果",res)
            },
            error=>{
              console.log("b",error.message)
            }
          )
        },
        // 时间转换
        formatDateTime (date) {
          var date = new Date(date)
          var y = date.getFullYear();
          var m = date.getMonth() + 1;
          m = m < 10 ? ('0' + m) : m;
          var d = date.getDate();
          d = d < 10 ? ('0' + d) : d;
          // return y + '-' + m + '-' + d+' '+h+':'+minute;
          return y + '-' + m + '-' + d
        },
        //结束时间设置
        endtime(){
          let nowData1 = this.formatDateTime(new Date()).slice(0,8)
          let nowData2 = this.formatDateTime(new Date()).slice(8)
          let nowData3 = String(new Date()).slice(16,18)
          let nowData4 = String(new Date()).slice(18,24)
          nowData3 = Number(nowData3) + 6>24 ? '0' + (Number(nowData3) + 6)%24 : Number(nowData3)+6
          if(nowData3+6>24){
            nowData2 = Number(nowData2)+1>=10 ? Number(nowData2)+1 : '0'+(Number(nowData2)+1)
          }
          this.endData = nowData1 + nowData2 + ' '+nowData3 + nowData4
          console.log('支付结束时间为：',this.endData,nowData1,nowData2,nowData3,nowData4)
        },
      // 生成订单,并更新用户余额
      addOrder(){
        let nextDate = new Date(this.endTime);
        let currDate,currTime,nextTime,allTime,allSecond;
        currDate = new Date();
        currTime = currDate.getTime();
        nextTime = nextDate.getTime();
        allTime = nextTime - currTime;
        allSecond = Math.floor(allTime/1000);
        this.allSecond = allSecond
        console.log("生成订单需要的信息为：",allSecond,this.tableData,this.orderList)
        this.endtime()
        // 生成订单 //竞拍结束、订单未生成、有用户拍拍卖
        if(allSecond<=0 && this.tableData!=''&&this.orderList == ''){
            axios.get("http://localhost:8080/api/addOrder/addOrderlist",{
              params:{
                goodsId:this.goodsId,
                goodsName:this.goodsList.goodsName,
                maxPrice:this.firstUser.price,
                state:0,
                createTime:this.formatDateTime(new Date()) + ' ' + String(new Date()).slice(16,24),
                userId:this.firstUser.userId,
                userName:this.userName,
                goodsImg:this.goodsList.goodsImg,
                countdown:0,
                endTime:this.endData,
              }
            }).then(
              res=>{
                this.$message({
                  message:'订单已经生成,请注意支付时间,未在规定时间支付后果自负',
                  type:'success'
                })
              },
              error=>{
                console.log("b",error.message)
              }
            );
            //更新商品拍卖状态---成功/流拍
            axios.get("http://localhost:8080/api/updateGoodsPai/updateGoodsPailist",{
              params:{
                isPaimai:1,
                goodsId:this.goodsId
              }
            }).then(
              res=>{
                this.$message({
                  message:'该商品状态已更新',
                  type:'success'
                })
              },
              error=>{
                console.log("b",error.message)
              }
            );
            for(var i =1;i<this.tableData.length;i++){
              // 获取未竞拍成功的用户余额
              axios.get("http://localhost:8080/api/getUserMoney/getUserMoneylist",{
                params:{
                  userId:this.tableData[i].userId,
                }
              }).then(
                res=>{
                  this.userMoney.push(res.data[0].money)
                  this.userMoney = JSON.parse(JSON.stringify(this.userMoney));
                  console.log("余额",this.userMoney,res.data[0].money,this.userMoney[0])
                },
                error=>{
                  console.log("88",error.message)
                }
              )
              this.flag = true
              // allMoney = this.goodsList.goodsPrice + this.userMoney
              //更新未竞拍成功用户的押金返回 
            }
            console.log("已经生成订单")
        }else{
          console.log("商品竞拍还未结束/订单已经生成，不会重复生成订单/无人参加竞拍")
          // 退还未竞拍成功用的押金
          // axios.get("http://localhost:8080/api/updateGoodsPai/updateGoodsPailist",{
          //     params:{
          //       isPaimai:1,
          //       goodsId:this.goodsId
          //     }
          //   }).then(
          //     res=>{
          //       this.$message({
          //         message:'该商品状态已更新',
          //         type:'success'
          //       })
          //     },
          //     error=>{
          //       console.log("b",error.message)
          //     }
          //   );
        }
        // 流拍
        if(allSecond<=0 && this.tableData ==''){
          //更新商品拍卖状态---成功/流拍
          axios.get("http://localhost:8080/api/updateGoodsPai/updateGoodsPailist",{
            params:{
              isPaimai:0,
              goodsId:this.goodsId
            }
          }).then(
            res=>{
              this.$message({
                message:'该商品状态已更新',
                type:'success'
              })
            },
            error=>{
              console.log("b",error.message)
            }
          );
        }
      },
      // 更新用户余额---退押金
      updatedMoney(){
        this.timer = setTimeout(()=>{
          if(this.allSecond<=0 && this.tableData!=''&&this.flag){
            this.userMoney = JSON.parse(JSON.stringify(this.userMoney));
            console.log("用户剩余余额为",this.userMoney)
            for(var i=1;i<this.tableData.length;i++){
              axios.get("http://localhost:8080/api/updateUserMoney/updateUserMoneylist",{
                params:{
                  userId:this.tableData[i].userId,
                  money:this.goodsList.goodsPrice + this.userMoney[i-1]
                }
              }).then(
                res=>{
                  this.$message({
                    message:'定金已退回',
                    type:'success'
                  })
                },
                error=>{
                  console.log("88",error.message)
                }
              )
              axios.get("http://localhost:8080/api/addMoneyChange/addMoneyChangelist",{
                params:{
                  userId:this.tableData[i].userId,
                  moneychange:`已经退回押金${this.goodsList.goodsPrice}元`
                }
              }).then(
                res=>{

                },
                error=>{
                  console.log("88",error.message)
                }
              )
              this.flag = false
            }
          }
        },1000) 
      },
      // 获取用户竞拍信息
      getAuction(){
        axios.get("http://localhost:8080/api/getUserAuction/getUserAuctionlist",{
            params:{
              goodsId:this.goodsId
            }
          }).then(
            res=>{
              for(var i= 0;i<res.data.length;i++){
                    var startTemp = Number(String(res.data[i].nowTime).slice(11,13))+8
                    var phone = res.data[i].userPhone
                    var startHour = startTemp%24 >= 10? startTemp%24 :'0'+startTemp%24
                    res.data[i].nowTime = String(res.data[i].nowTime).slice(0,10)+ ' ' + startHour + String(res.data[i].nowTime).slice(13,19)
                    res.data[i].userPhone = String(phone).slice(0,3) + ' **** ' + String(phone).slice(7,11)
                    // state.usersubgoods.push(value [i])   
                }
              this.tableData = res.data
              this.tableData.sort(function(x,y) {//大-小，降序排列，对于最新时间是升序
                  return y.price-x.price;
              })
              this.firstUser = this.tableData[0]
              console.log("999",res)
              console.log("用户竞拍信息为,以及最高价用户",this.tableData,this.firstUser)
            },
            error=>{
              console.log("88",error.message)
            }
          )
      },
    },
    watch:{
        goodsList:function(newData){
            this.goodsList = newData;
        }
    }
};
</script>

<style scoped>

</style>